package com.vpx.server.service.system.impl;

import com.vpx.server.bean.info.system.MenuOperateInfo;
import com.vpx.server.bean.result.base.BaseDeleteResult;
import com.vpx.server.framework.aop.ServiceHandler;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.exception.BizException;
import com.vpx.server.bean.entity.SysMenu;
import com.vpx.server.mapper.system.SysMenuMapper;
import com.vpx.server.mapper.system.SysMenuOperateMapper;
import com.vpx.server.bean.order.base.BaseDeleteOrder;
import com.vpx.server.bean.result.system.SysMenuListResult;
import com.vpx.server.bean.result.system.SysMenuResult;
import com.vpx.server.bean.result.system.SysMenuTreeResult;
import com.vpx.server.service.system.SysMenuService;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.utils.UserLoginUtils;
import com.vpx.server.bean.order.base.BaseIdOrder;
import com.vpx.server.bean.order.system.SysMenuOrder;
import com.vpx.server.bean.order.system.SysMenuParentIdOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module 菜单
 * @description: 菜单业务
 * @date 2021/9/7 19:15
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysMenuOperateMapper sysMenuOperateMapper;

    @Override
    @ServiceHandler(memo = "新增菜单", isTransaction = true)
    public ResultMsg insert(SysMenuOrder order) {
        int count = sysMenuMapper.querySysMenuCountByCode(order.getCode());
        if (count > 0){
            throw new BizException("菜单编码已存在");
        }
        SysMenu menu = new SysMenu();
        menu.from(order);
        menu.setCreateUserId(UserLoginUtils.getCurrentUser().getId());
        menu.setUpdateUserId(UserLoginUtils.getCurrentUser().getId());
        menu.setCreateTime(DateUtils.now());
        menu.setUpdateTime(DateUtils.now());
        sysMenuMapper.insert(menu);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "修改菜单", isTransaction = true)
    public ResultMsg updateById(SysMenuOrder order) {
        SysMenu menu = sysMenuMapper.selectById(order.getId());
        if (menu == null) {
            throw new BizException("未查询到菜单信息");
        }
        if(checkCodeIsExists(order)){
            throw new BizException("菜单编码已存在");
        }
        menu.from(order);
        menu.setUpdateUserId(UserLoginUtils.getCurrentUser().getId());
        menu.setUpdateTime(DateUtils.now());
        sysMenuMapper.updateById(menu);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "删除菜单", isTransaction = true)
    public ResultMsg<BaseDeleteResult> deleteById(BaseDeleteOrder order) {
        BaseDeleteResult result = new BaseDeleteResult();
        result.setDeleteConfirm(false);
        if(!order.getIsConfirm()){
            boolean bool = hasChild(order.getId());
            List<MenuOperateInfo> menuOperateList = sysMenuOperateMapper.querySysMenuOperateByMenuId(order.getId());
            if(bool || !menuOperateList.isEmpty()){
                result.setDeleteConfirm(true);
            }else {
                sysMenuMapper.deleteById(order.getId());
            }
        }else {
            delMenuAndChild(order.getId());
        }
        return ResultMsg.success(result);
    }

    @Override
    @ServiceHandler(memo = "查询菜单")
    public ResultMsg<SysMenuResult> findById(BaseIdOrder order) {
        SysMenu menu = sysMenuMapper.selectById(order.getId());
        if (menu == null) {
            throw new BizException("未查询到菜单信息");
        }
        SysMenuResult menuResult = new SysMenuResult();
        menuResult.from(menu);
        return ResultMsg.success(menuResult);
    }

    @Override
    @ServiceHandler(memo = "查询菜单列表")
    public ResultMsg<SysMenuListResult> findListByParentId(SysMenuParentIdOrder order) {
        List<SysMenuTreeResult> menuTreeList = null;
        if(order.isHasMenuOperate()){
            menuTreeList = queryMenuAndOperateTreeByParentId(order.getParentId());
        }else {
            menuTreeList = queryMenuTreeByParentId(order.getParentId());
        }
        SysMenuListResult menuListResult = new SysMenuListResult();
        menuListResult.setList(menuTreeList);
        return ResultMsg.success(menuListResult);
    }

    @ServiceHandler(memo = "查询菜单树形列表")
    public List<SysMenuTreeResult> queryMenuTreeByParentId(Long parentId) {
        List<SysMenu> sysMenus = sysMenuMapper.querySysMenuByParentId(parentId);
        List<SysMenuTreeResult> menuTreeResults = new ArrayList<SysMenuTreeResult>();
        boolean hasChild = false;
        SysMenuTreeResult menuTreeResult = null;
        for (SysMenu menu : sysMenus){
            hasChild  = hasChild(menu.getId());
            menuTreeResult = new SysMenuTreeResult();
            menuTreeResult.from(menu);
            menuTreeResults.add(menuTreeResult);
            if(hasChild){
                menuTreeResult.setIsparent(true);
                List<SysMenuTreeResult> list = queryMenuTreeByParentId(menu.getId());
                menuTreeResult.setChildList(list);
            }
        }
        return menuTreeResults;
    }

    @ServiceHandler(memo = "查询菜单及菜单操作按钮树形列表")
    public List<SysMenuTreeResult> queryMenuAndOperateTreeByParentId(Long parentId) {
        List<SysMenu> sysMenus = sysMenuMapper.querySysMenuByParentId(parentId);
        List<SysMenuTreeResult> menuTreeResults = new ArrayList<SysMenuTreeResult>();
        boolean hasChild = false;
        SysMenuTreeResult menuTreeResult = null;
        List<MenuOperateInfo> menuOperateInfos = null;
        for (SysMenu menu : sysMenus){
            menuTreeResult = new SysMenuTreeResult();
            menuOperateInfos = sysMenuOperateMapper.querySysMenuOperateByMenuId(menu.getId());
            menuTreeResult.setMenuOperateInfos(menuOperateInfos);
            hasChild  = hasChild(menu.getId());
            menuTreeResult.from(menu);
            menuTreeResults.add(menuTreeResult);
            if(hasChild){
                menuTreeResult.setIsparent(true);
                List<SysMenuTreeResult> list = queryMenuAndOperateTreeByParentId(menu.getId());
                menuTreeResult.setChildList(list);
            }
        }
        return menuTreeResults;
    }

    @ServiceHandler(memo = "查询菜单是否存在子菜单")
    public boolean hasChild(Long parentId){
        int count = sysMenuMapper.querySysMenuChildCount(parentId);
        return count > 0 ? true : false;
    }

    @ServiceHandler(memo = "检查菜单操作编码是否存在")
    public boolean checkCodeIsExists(SysMenuOrder order){
        int count = sysMenuMapper.querySysMenuCountByIdAndCode(order.getId(), order.getCode());
        if(count == 1){
            return false;
        }
        int num = sysMenuMapper.querySysMenuCountByCode(order.getCode());
        if (num > 0){
            return true;
        }
        return false;
    }

    @ServiceHandler(memo = "删除菜单及子菜单",isTransaction = true)
    private void delMenuAndChild(Long id){
        List<SysMenu> sysMenus = sysMenuMapper.querySysMenuByParentId(id);
        for (SysMenu menu : sysMenus){
            if(hasChild(menu.getId())){
                delMenuAndChild(menu.getId());
            }
            sysMenuMapper.deleteById(menu.getId());
            sysMenuOperateMapper.deleteMenuOperateByMenuId(id);
        }
        sysMenuMapper.deleteById(id);
        sysMenuOperateMapper.deleteMenuOperateByMenuId(id);
    }

}
